#!/usr/bin/env node

// 引入所需的模块
const http = require('http');

// 创建 HTTP 服务器
const server = http.createServer((req, res) => {
  console.log(`Request Line: ${req.method} ${req.url}`);

  // 打印请求头
  console.log('Request Headers:', req.headers);

  let body = '';
  req.on('data', chunk => {
    body += chunk.toString(); // 将请求体转换为字符串
    console.log(`BODY: ${chunk}`); // 打印请求体的每个部分
  });

  req.on('end', () => {
    console.log('No more data in request.');
    // 发送响应
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello, you sent data to the server!');
  });
});

// 监听 8080 端口
server.listen(8080, () => {
  console.log('Server listening on port 8080\n');
});